﻿//
// Copyright (c) 2024 BPer
//
#ifndef SRC_BPERPLAYER_SRC_BPERRTCRECEIVE_H_
#define SRC_BPERPLAYER_SRC_BPERRTCRECEIVE_H_

#include <bperutil/buffer/BPerAudioEncoderBuffer.h>
#include <bperutil/buffer/BPerVideoDecoderBuffer.h>
#include <bperutil/sys/BPerSysMessageI.h>
#include <bperutil/sys/BPerThread2.h>
#include <bperutil/BPerAvInfoType.h>

#include <bperrtc/BPerPeerConnection.h>
#include <bperavutil/video/BPerNalu.h>





using namespace std;
class BPerRtcReceive : public BPerThread,public BPerMediaConfigCallback{
public:
	BPerRtcReceive(BPerContext* pcontext,BPerSysMessageI* pmessage);
	virtual ~BPerRtcReceive();
	void receiveAudio(BPerFrame* audioFrame);
	void receiveVideo(BPerFrame* videoFrame);

	int32_t publishMsg(BPerFrame* msgFrame);
	int32_t receiveMsg(BPerFrame* msgFrame);
    int32_t init(int32_t puid,char* localIp, char* server, int32_t pport,char* app,	char* stream);
    int32_t init(int32_t puid,char* url);
    void setBuffer(BPerAudioEncoderBuffer *al,BPerVideoDecoderBuffer *vl);
    void disConnect();
    void play(char* pserverStr,char *streamName);
    void setMediaConfig(int32_t puid, BPerAudioParam *audio,BPerVideoParam *video);
    BPerPeerConnection *m_recv;
  	int32_t m_isReceived; //,isHandled;
	int32_t m_loops; //,isHandleAllInvoke;
	int32_t m_isStart;
	void stop();
protected:
	void run();
	void startLoop();
	BPerContext* m_context;
	BPerSysMessageI* m_message;



private:
	bPer_thread_mutex_t m_lock;
	bPer_thread_cond_t m_cond_mess;
	//BPerStreamConfig m_conf;
	int32_t m_waitState;
	int32_t m_headLen;
	BPerAudioEncoderBuffer *m_out_audioBuffer;
	BPerVideoDecoderBuffer *m_out_videoBuffer;
	BPerRtcInfo m_rtcinfo;
	BPerH264NaluData m_nalu;
	//BPerReceiveCallback m_recvCallback;
    char* m_url;
    //uint8_t* m_keyBuf;
};

#endif /* SRC_BPERPLAYER_SRC_BPERRTCRECEIVE_H_ */
